import React, { Component } from 'react'
import { NavBar, Icon, InputItem, Button, Toast } from 'antd-mobile';
import './Login.less'
import { Link } from 'react-router-dom'
import { reqLogin } from '../../http/api'
export default class Login extends Component {
    constructor() {
        super()
        // 初始化数据
        this.state = {
            user: {
                phone: "123",
                password: "123"
            }
        }
    }
    // 2.绑定数据
    ChangeUser(val, key) {
        console.log(val);
        this.setState({
            user: {
                ...this.state.user,
                [key]: val
            },
            type:1
        })
    }
    gohome() {
            //某一个登录权限




        // 发请求
        reqLogin(this.state.user).then(res => {
            if (res.data.code === 200) {
                // 弹成功
                Toast.success(res.data.msg, 1);
                // 存用户信息
                sessionStorage.setItem('user',JSON.stringify(res.data.list))
                // 跳转页面
                this.props.history.push('/index/home')
            }
        })
    }


    render() {
        let { user,type } = this.state
        return (
            <div>
                <NavBar
                    mode="dark"
                    icon={<Icon type="left" />}
                >登录</NavBar>

                <div className="enter_user">

                    角色：<select name="" id="">
                        <option value="" disabled>请选择角色</option>
                        <option value={type}>法师</option>
                        <option value="2">打野</option>
                    </select>
                    <InputItem
                        clear
                        placeholder="123"

                        onChange={(val) => this.ChangeUser(val, "phone")}
                    >账号</InputItem>


                    <InputItem

                        onChange={(val) => this.ChangeUser(val, "password")}
                        clear
                        placeholder="123"
                    >密码</InputItem>
                    <Button onClick={() => this.gohome()} className="btn_login" type="primary" inline style={{ marginRight: '4px' }}>登录</Button>
                    <div className="tologin">
                        <span>还没有账号，去注册</span>
                    </div>
                </div>
                <div>{JSON.stringify(user)}</div>
                <Link to="/index/home">去首页</Link>

            </div>
        )
    }
}
